// @algorithm @lc id=352 lang=cpp 
// @title data-stream-as-disjoint-intervals
class SummaryRanges {
	map<int, int> _sec;
public:
    SummaryRanges() {}
    
    void addNum(int val) {
		auto R = _sec.upper_bound(val); 
		auto L = _sec.begin() == R ? _sec.end() : prev(R);
		if(L!=_sec.end() && L->first <= val && val <= L->second)
			return;
		bool aL = L!=_sec.end() && val==L->second + 1;
		bool bR = R!=_sec.end() && val==R->first  - 1;
		if(aL && bR){
			L->second = R->second;
			_sec.erase(R);
			return;
		}
		if(aL){
			L->second++;
			return;
		}
		if(bR){
			_sec[val] = R->second;
			_sec.erase(R);
			return;
		}
		_sec[val] = val;
    }
    
    vector<vector<int>> getIntervals() {
		vector<vector<int>> sec;
		for(auto & s : _sec)
			sec.push_back({s.first, s.second});
		return sec;
    }
};